package com.movile.checkhoras.android.widget;


import com.movile.checkhoras.android.R;
import com.movile.checkhoras.android.dao.TableCheckHorasHelper;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.RemoteViews;


public class CheckWidget1Provider extends AppWidgetProvider {

   
    TableCheckHorasHelper tableCheckHoras;
    
    


    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
                                
        tableCheckHoras = new TableCheckHorasHelper(context);
        
        

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];                        
            
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_1_layout);
            
            updateCheck(views);
            
            
            
            Intent intent = new Intent(context,UpdateWidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]);            
            PendingIntent pendingIntent = PendingIntent.getService(context, appWidgetId, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
            
            views.setOnClickPendingIntent(R.id.btWidgetCheck, pendingIntent);
            
                                    
            
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
    
    private void updateCheck(RemoteViews views){
        if(tableCheckHoras.IsCheckInLast()){
            views.setTextViewText(R.id.txtWidgetCheck, "CheckOut");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkoutwid);
        } else {
            views.setTextViewText(R.id.txtWidgetCheck, "CheckIn");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkinwid);           
        }
    }
}
